﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace AISFMIOperator
{
    class Address
    {
        public Address()
        { }

        public int AddressID
        { get; set; }

        public string Addr
        { get; set; }

        public City City
        { get; set; }

        public int PostIndex
        { get; set; }

        public string FullAddress
        {
            get 
            {
                return Addr + ", " + PostIndex + ", " + City.CityName + ", " + City.Region.RegionName;
            }
        }

        public void Save()
        {
            SqlCommand command = new SqlCommand("CreateAddress");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@address", SqlDbType.VarChar, 128).Value = Addr;
            command.Parameters.Add("@post_index", SqlDbType.Int).Value = PostIndex;
            command.Parameters.Add("@city_id", SqlDbType.Int).Value = City.CityID;
            command.Parameters.Add("@address_id", SqlDbType.Int).Direction = ParameterDirection.Output;
            SQLDBLayer.ExecSPNonQuery(command);
            this.AddressID = (int)command.Parameters["@address_id"].Value;
        }

        public void Update()
        {
            SqlCommand command = new SqlCommand("UpdateAddress");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@address", SqlDbType.VarChar, 128).Value = Addr;
            command.Parameters.Add("@post_index", SqlDbType.Int).Value = PostIndex;
            command.Parameters.Add("@city_id", SqlDbType.Int).Value = City.CityID;
            command.Parameters.Add("@address_id", SqlDbType.Int).Value = AddressID;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public void Delete()
        {
            SqlCommand command = new SqlCommand("DeleteAddress");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@address_id", SqlDbType.Int).Value = AddressID;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public static List<Address> GetAllAddresses()
        {
            DataTable table = new DataTable();
            List<Address> objects = new List<Address>();
            SqlCommand command = new SqlCommand("GetAddresses");
            command.CommandType = CommandType.StoredProcedure;

            table = SQLDBLayer.ExecSPTable(command);

            foreach (DataRow row in table.Rows)
            {
                Address addr = new Address();
                addr.AddressID = (int)row["address_id"];
                addr.PostIndex = (int)row["post_index"];
                addr.Addr = row["address"].ToString();

                int city_id = (int)row["city_id"];

                addr.City = (City)City.GetAllCities().Where(o => o.CityID == city_id).ToList()[0];

                objects.Add(addr);
            }

            return objects;
        }
    }
}
